package io.github.javpower.gifweb.controller;

import io.github.javpower.gifweb.entity.Template;
import io.github.javpower.gifweb.service.TemplateService;
import io.github.javpower.gifweb.util.GifTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

//用户使用展示页面
@RestController
@RequestMapping("/api/web")
public class TemplateWebController {

    @Autowired
    private TemplateService templateService;

    @PostMapping("/create")
    public ResponseEntity<String> createTemplate(@RequestParam("templateId") String templateId,@RequestParam("overlayImage") MultipartFile overlayImageFile) {
        try {
            Template template = templateService.getTemplate(templateId);
            String base64OutputGif = GifTemplateUtil.applyTemplateToGif(template.getGifBase64(), template.getTemplateParams(), overlayImageFile);
            return ResponseEntity.ok(base64OutputGif);
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("文件处理失败: " + e.getMessage());
        }
    }
    @PostMapping("/list")
    public ResponseEntity<List<Template>> getAllTemplates() {
        return ResponseEntity.ok(templateService.getAllTemplates());
    }

}